<template>
  <div>
    <div class="query-form">
      <el-form
        :inline="true"
        size="small"
        :model="menuForm"
        ref="menuForm"
        class="demo-form-inline"
      >
        <el-form-item label="菜单名称" prop="menuName">
          <el-input
            v-model="menuForm.menuName"
            placeholder="请输入菜单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-select v-model="menuForm.menuState" placeholder="请选择菜单状态">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="停用" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchMenu">查询</el-button>
          <el-button @click="resetForm('menuForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="base-table">
      <!-- 新增按钮 -->
      <div class="action">
        <el-button type="primary" size="small" @click="handleOpen(1)"
          >新增</el-button
        >
      </div>
      <!-- 菜单列表 -->
      <el-table
        size="mini"
        :data="menuList"
        style="width: 100% margin-bottom:20px;"
        row-key="_id"
        :tree-props="{ children: 'children' }"
      >
        >
        <el-table-column
          v-for="item in tableList"
          :key="item._id"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :formatter="item.formatter"
        >
        </el-table-column>
        <el-table-column label="操作" width="230px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleOpen(2, scope.row)"
              >新增</el-button
            >
            <el-button size="mini" @click="editMenu(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delMenu(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 菜单弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="menu" :rules="rules" ref="menu" label-width="100px">
        <el-form-item label="父级菜单" prop="parentId">
          <el-cascader
            placeholder="请选择父级菜单"
            v-model="menu.parentId"
            :options="menuList"
            :props="{ checkStrictly: true, label: 'menuName', value: '_id' }"
            clearable
          ></el-cascader>
          <span>不选, 则直接创建一级菜单</span>
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="menu.menuType">
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input
            v-model="menu.menuName"
            autocomplete="off"
            placeholder="请输入菜单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon" v-show="menu.menuType == '1'">
          <el-input
            v-model="menu.icon"
            autocomplete="off"
            placeholder="请输入菜单图标"
          ></el-input>
        </el-form-item>
        <el-form-item label="路由地址" prop="path" v-show="menu.menuType == '1'">
          <el-input
            v-model="menu.path"
            autocomplete="off"
            placeholder="请输入路由地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="component" v-show="menu.menuType == '1'">
          <el-input
            v-model="menu.component"
            autocomplete="off"
            placeholder="请输入组件路径"
          ></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState" v-show="menu.menuType == '1'">
          <el-radio-group v-model="menu.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限标识" prop="menuCode" v-show="menu.menuType == '2'">
          <el-input
            v-model="menu.menuCode"
            autocomplete="off"
            placeholder="请输入权限标识"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddAndEditMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      title: "",
      menuForm: {
        menuName: "",
        menuState: "",
      },
      menuList: [],
      dialogFormVisible: false,
      menu: {
        action: "",
        parentId: [],
        menuType: 1,
        menuCode: "",
        path: "",
        icon: "",
        component: "",
        menuState: 1,
        menuName: "",
      },
      rules: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
        ],
      },
      tableList: [
        {
          label: "菜单名称",
          prop: "menuName",
        },
        {
          label: "图标",
          prop: "icon",
          width: "180px",
        },
        {
          label: "菜单类型",
          prop: "menuType",
          formatter(row, column, value) {
            let type = {
              1: "菜单",
              2: "按钮",
            };
            return type[value];
          },
        },
        {
          label: "权限标识",
          prop: "menuCode",
        },
        {
          label: "路由地址",
          prop: "path",
        },
        {
          label: "组件路径",
          prop: "component",
        },
        {
          label: "菜单状态",
          prop: "menuState",
          formatter(row, column, value) {
            let state = {
              1: "正常",
              2: "停用",
            };
            return state[value];
          },
        },
        {
          label: "创建时间",
          prop: "createTime",
          width: "180px",
          formatter(row, cloumn, value) {
            return moment
              .parseZone(value)
              .local()
              .format("YYYY-MM-DD HH:mm:ss");
          },
        },
      ],
    };
  },
  mounted() {
    this.getMenuList();
  },
  methods: {
    async getMenuList() {
      let res = await this.menuApi.getMenuList(this.menuForm);
      this.menuList = res;
    },
    searchMenu() {
      this.getMenuList();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.getMenuList();
    },
    handleOpen(type, row) {
      this.dialogFormVisible = true;
      this.menu.action = "add";
      this.title = "菜单新增";
      if (type === 2) {
        this.menu.parentId = [...row.parentId, row._id].filter((item) => {
          return item;
        });
      } else {
        this.menu.parentId = [];
      }
    },
    AddAndEditMenu() {
      this.$refs["menu"].validate(async (valid) => {
        if (valid) {
          let res = await this.menuApi.AddAndEditMenu(this.menu);
          console.log(res);
          this.dialogFormVisible = false;
          this.getMenuList();
        } else {
          this.$message.error({
            message: "输入内容有误,请重新输入",
            offset: 100,
          });
        }
      });
    },
    editMenu(row){
        this.dialogFormVisible = true
        this.$nextTick(()=>{
            Object.assign(this.menu, row);
        })
        this.menu.action = 'edit';
    },
    async delMenu(id){
        let res = await this.menuApi.AddAndEditMenu({action: 'delete', _id: id});
        this.getMenuList();
        this.$message.success({
            message: '删除成功',
            offset: 100,
        })
    }
  },
};
</script>

<style scoped >
</style>
